﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using CMD_DKT.BAL;
using System.Windows.Controls;
using System.Threading;
using System.Windows;
using System.Collections;

namespace CMD_DKT.Utilities
{
    class ThreadUI
    {
        
        public static Thread t = null;

        public static System.Windows.Controls.ContentControl pnl;
        public static CMDPopUp pop;
        public static Solicitud sol;

        public static bool CheckIsAlive()
        {

            if (ThreadUI.t != null)
            {
                 return ThreadUI.t.IsAlive;

            }
            return false;

        }/////////

        public static void save(Boolean Enviado)
        {
            //sol = new Solicitud();

            try
            {
                //GuardarSol.popup = pop;
                //solicitud= GuardarSol.Guardar();

                ArrayList info = CMDInfo.GetCMDInfo(Enviado);

                // ----------   DOCINFO
                //sol.SetDictionaryParameterValues("docInfo_unidDocInfo", lbl_unidDocinfo.Content.ToString());
                foreach (var item in info)
                {
                    string[] i = (string[])item;
                    sol.SetDictionaryParameterValues(i[0], i[1]);
                }
                try
                {

                    string msg = "";
                    DataSet result = sol.UpsertData();

                    if (result.Tables.Count == 1)
                        msg = "Se ha guardado su solicitud.";

                    if (result.Tables.Count == 2)
                        msg = result.Tables[0].Rows[0][0].ToString() + ": " + result.Tables[1].Rows[0][0].ToString();
                    if (result.Tables.Count == 3)
                        msg = result.Tables[1].Rows[0][0].ToString() + "\n " + result.Tables[0].Rows[0][0].ToString() + "\n " + result.Tables[2].Rows[0][0].ToString();
                    if (result.Tables.Count == 0)
                        msg = "Se ha realizado la actualización.";

                    //System.Windows.MessageBox.Show(msg, "Mensaje del sistema", System.Windows.MessageBoxButton.OK, System.Windows.MessageBoxImage.Information,MessageBoxResult.OK, MessageBoxOptions.DefaultDesktopOnly);
                    // return sol;
                }
                catch (Exception ex)
                {
                    throw ex;
                }

            }
            catch (Exception ex)
            {
                throw ex;
            }

        }/////////

        public static void start(Solicitud solicitud, MainWindow mw,bool salir)
        {
            sol = solicitud;
            try
            {
                //pop = new CMDPopUp();
                //pop.showPopup("Guardando solicitud...", pnl);
                mw.lbl_Saving.Content = "Guardando solicitud...";
                mw.BloqueoMenus(false);
                if (!ThreadUI.CheckIsAlive())
                {   

                    ThreadUI.t = new Thread(() => {
                        ThreadUI.save(false);
                        //Action a = () => pop.HidePopup();
                        Action a = () => mw.lbl_Saving.Content="";
                        Application.Current.Dispatcher.BeginInvoke(a, System.Windows.Threading.DispatcherPriority.Background);
                        Action b = () => mw.BloqueoMenus(true);
                        Application.Current.Dispatcher.BeginInvoke(b, System.Windows.Threading.DispatcherPriority.Background);
                        if (salir)
                        {
                            a = () => mw.lblSolicitudClientes_MouseUp(mw.lblSolicitudClientes, null);
                            Application.Current.Dispatcher.BeginInvoke(a, System.Windows.Threading.DispatcherPriority.Background);
                        }
                    });

                    ThreadUI.t.IsBackground = true;

                    ThreadUI.t.Start();

                }

            }
            catch (Exception ex)
            {
                throw ex;

            }
            
            //return solicitud;

        }/////////

 
    }//class
}
